package com.car.rental.common.core;

import lombok.Data;
import lombok.experimental.Accessors;

import java.io.Serializable;

/**
 * 统一返回结果类
 *
 * @author Your Name
 * @param <T> 数据泛型
 */
@Data
@Accessors(chain = true)
public class R<T> implements Serializable {
    private static final long serialVersionUID = 1L;

    /** 状态码 */
    private int code;
    
    /** 返回消息 */
    private String msg;
    
    /** 返回数据 */
    private T data;

    /**
     * 返回成功结果
     *
     * @param <T> 数据泛型
     * @return 成功结果
     */
    public static <T> R<T> ok() {
        return restResult(null, 200, "操作成功");
    }

    /**
     * 返回带数据的成功结果
     *
     * @param data 数据
     * @param <T> 数据泛型
     * @return 成功结果
     */
    public static <T> R<T> ok(T data) {
        return restResult(data, 200, "操作成功");
    }

    public static <T> R<T> ok(String msg) {
        return restResult(null, 200, msg);
    }

    public static <T> R<T> ok(T data, String msg) {
        return restResult(data, 200, msg);
    }

    public static <T> R<T> fail() {
        return restResult(null, 500, "操作失败");
    }

    public static <T> R<T> fail(String msg) {
        return restResult(null, 500, msg);
    }

    public static <T> R<T> fail(T data, String msg) {
        return restResult(data, 500, msg);
    }

    private static <T> R<T> restResult(T data, int code, String msg) {
        R<T> r = new R<>();
        r.setCode(code);
        r.setData(data);
        r.setMsg(msg);
        return r;
    }
} 